{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8a8225dc-c9b0-4f5f-ba70-2e911a636073",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting neo4j\n",
      "  Downloading neo4j-5.28.2-py3-none-any.whl.metadata (5.9 kB)\n",
      "Collecting pytz (from neo4j)\n",
      "  Downloading pytz-2025.2-py2.py3-none-any.whl.metadata (22 kB)\n",
      "Downloading neo4j-5.28.2-py3-none-any.whl (313 kB)\n",
      "Downloading pytz-2025.2-py2.py3-none-any.whl (509 kB)\n",
      "Installing collected packages: pytz, neo4j\n",
      "Successfully installed neo4j-5.28.2 pytz-2025.2\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install neo4j"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7ed18cf0-95d0-4d81-a454-635668ca8b6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from neo4j import GraphDatabase, exceptions\n",
    "class Neo4jConnection:\n",
    "    def __init__(self, uri, user, password):\n",
    "        self.driver = GraphDatabase.driver(uri, auth=(user, password))\n",
    "    \n",
    "    def close(self):\n",
    "        # 关闭连接\n",
    "        self.driver.close()\n",
    "    \n",
    "    def execute_query(self, query, parameters=None, db=None):\n",
    "        \"\"\"执行Cypher查询并返回结果\"\"\"\n",
    "        session = None\n",
    "        response = None\n",
    "        try:\n",
    "            session = self.driver.session(database=db) if db else self.driver.session()\n",
    "            response = list(session.run(query, parameters))\n",
    "        except exceptions.Neo4jError as e:\n",
    "            print(f\"查询执行错误: {e}\")\n",
    "        finally:\n",
    "            if session:\n",
    "                session.close()\n",
    "        return response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7be90f49-ec28-4f52-8ae6-87615708771d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Neo4j数据库配置\n",
    "NEO4J_URI = \"bolt://localhost:7687\"  # 默认bolt协议端口\n",
    "NEO4J_USER = \"neo4j\"                 # 默认用户名\n",
    "NEO4J_PASSWORD = \"n4j.123456\"     # 安装时设置的密码\n",
    "# 建立连接\n",
    "conn = Neo4jConnection(NEO4J_URI, NEO4J_USER, NEO4J_PASSWORD)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6888ac0f-80d7-4064-a6f7-04ea498ddf12",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的节点:\n",
      "姓名: Alice, 年龄: 30\n"
     ]
    }
   ],
   "source": [
    "# 1. 创建节点示例\n",
    "create_query = \"\"\"\n",
    "CREATE (p:Person {name: $name, age: $age})\n",
    "RETURN p.name AS name, p.age AS age\n",
    "\"\"\"\n",
    "params = {\"name\": \"Alice\", \"age\": 30}\n",
    "result = conn.execute_query(create_query, params)\n",
    "print(\"创建的节点:\")\n",
    "for record in result:\n",
    "    print(f\"姓名: {record['name']}, 年龄: {record['age']}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1b2f1995-ffa2-4d78-b108-df0dc223ac57",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "查询到的节点:\n",
      "姓名: Alice, 年龄: 30\n"
     ]
    }
   ],
   "source": [
    "# 2. 查询节点示例\n",
    "match_query = \"\"\"\n",
    "MATCH (p:Person)\n",
    "WHERE p.age > $min_age\n",
    "RETURN p.name AS name, p.age AS age\n",
    "\"\"\"\n",
    "params = {\"min_age\": 25}\n",
    "result = conn.execute_query(match_query, params)\n",
    "print(\"\\n查询到的节点:\")\n",
    "for record in result:\n",
    "    print(f\"姓名: {record['name']}, 年龄: {record['age']}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9bc8afc5-7d5d-4942-ba70-632560ba6a1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "创建的关系:\n",
      "关系类型: FRIENDS_WITH\n"
     ]
    }
   ],
   "source": [
    "# 3. 创建关系示例\n",
    "create_relation_query = \"\"\"\n",
    "MATCH (a:Person {name: $name1}), (b:Person {name: $name2})\n",
    "CREATE (a)-[r:FRIENDS_WITH]->(b)\n",
    "RETURN type(r)\n",
    "\"\"\"\n",
    "# 先创建第二个节点\n",
    "conn.execute_query(create_query, {\"name\": \"Bob\", \"age\": 35})\n",
    "# 再创建关系\n",
    "result = conn.execute_query(create_relation_query, {\"name1\": \"Alice\", \"name2\": \"Bob\"})\n",
    "print(\"\\n创建的关系:\")\n",
    "for record in result:\n",
    "    print(f\"关系类型: {record['type(r)']}\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64adf971-1bea-4af1-8a47-3de422ce5aff",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
